import json

from flask import Blueprint, render_template, request,current_app,jsonify

from flask_login import login_required, current_user
from sqlalchemy import desc,func,case

from decimal import Decimal,getcontext
# 设置精度
getcontext().prec = 16
from applications.common import curd
from applications.common.curd import enable_status, disable_status
from applications.common.utils.http import table_api, fail_api, success_api
from applications.common.utils.rights import authorize
from applications.common.utils.validate import str_escape
from applications.common.utils.getapi import get_djbh,get_isSameMonth
from applications.extensions import db

from applications.models import *
from pinyin import pinyin
import datetime,os
from applications.common.utils import upload as upload_curd
bp = Blueprint('method', __name__, url_prefix='/method')

# 用户管理
@bp.get('/yp/')
@authorize("test:method:main:yp")
def main():
    return render_template('test/method/yp/main.html')

#   用户分页查询
@bp.get('/yp/data')
@authorize("test:method:main:yp")
def data():
    # 获取请求参数
    名称 = str_escape(request.args.get('名称', type=str))
    方法名称 = str_escape(request.args.get('方法名称', type=str))
    filters = []
    if 方法名称:
        filters.append(TestMethodYPModel.名称.contains(名称))

    if 方法名称:
        filters.append(TestMethodYPModel.方法名称.contains(方法名称))

    query = db.session.query(
        TestMethodYPModel
    ).filter(*filters).layui_paginate()

    return table_api(
        data=[{
            'id': item.id,
            '名称': item.名称,
            '检验项目': item.检验项目,
            '方法名称': item.方法名称,
            '方法依据': item.方法依据,
            'method_dh': item.method_dh,
            'date_time':item.date_time,


        } for item in query.items],
        count=query.total)

@bp.get('/yp/add')
@authorize("test:method:add:yp", log=False)
def add():
    '''先获取合格的供应商'''
    # raws = RawYPModel.query.filter_by(状态=1).all()
    return render_template('test/method/yp/add.html')